package com.ruoyi.web.controller.biz;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.biz.domain.BizAddress;
import com.ruoyi.biz.service.IBizAddressService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收货地址Controller
 * 
 * @author ruoyi
 * @date 2021-12-16
 */
@RestController
@RequestMapping("/biz/address")
public class BizAddressController extends BaseController
{
    @Autowired
    private IBizAddressService bizAddressService;

    /**
     * 查询收货地址列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BizAddress bizAddress)
    {
        startPage();
        List<BizAddress> list = bizAddressService.selectBizAddressList(bizAddress);
        return getDataTable(list);
    }

    /**
     * 导出收货地址列表
     */
    @PreAuthorize("@ss.hasPermi('system:address:export')")
    @Log(title = "收货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizAddress bizAddress)
    {
        List<BizAddress> list = bizAddressService.selectBizAddressList(bizAddress);
        ExcelUtil<BizAddress> util = new ExcelUtil<BizAddress>(BizAddress.class);
        util.exportExcel(response, list, "收货地址数据");
    }

    /**
     * 获取默认收货地址详细信息
     */
    @GetMapping(value = "/default/{userId}")
    public AjaxResult defaultAddress(@PathVariable("userId") long userId)
    {
        return AjaxResult.success(bizAddressService.selectDefaultAddress(userId));
    }

    /**
     * 获取收货地址详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:address:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizAddressService.selectBizAddressById(id));
    }

    /**
     * 设为默认地址
     */
    @GetMapping(value = "/config/{id}")
    public AjaxResult configDefault(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizAddressService.updateAddressDefault(id));
    }

    /**
     * 新增收货地址
     */
    @Log(title = "收货地址", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/add")
    public AjaxResult add(BizAddress bizAddress)
    {
        return toAjax(bizAddressService.insertBizAddress(bizAddress));
    }

    /**
     * 修改收货地址
     */
    @PreAuthorize("@ss.hasPermi('system:address:edit')")
    @Log(title = "收货地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizAddress bizAddress)
    {
        return toAjax(bizAddressService.updateBizAddress(bizAddress));
    }

    /**
     * 删除收货地址
     */
    @PreAuthorize("@ss.hasPermi('system:address:remove')")
    @Log(title = "收货地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizAddressService.deleteBizAddressByIds(ids));
    }
}
